﻿var popupStatus = 0;
var popupSaveStatus = 0;

////Carga el Background
//function CloseBack(){
//		$("#loadBackgroundPopup").fadeOut("slow");
//}

//Carga el Popup de Rechazo
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupReject").fadeIn("slow");
		popupStatus = 1;
	}
}

function loadPopupExit()
{
	var bp = top.frames["sommaire"].document.getElementById('backgroundSavePopup');
	var pe = top.frames["sommaire"].document.getElementById('popupExit');
	$(bp).css({
		"opacity": "0.7"
	});
	$(bp).fadeIn("slow");
	$(pe).fadeIn("slow");
}

//Carga el Popup de Guardado
function loadSavePopup(){
	//loads popup only if it is disabled
	if(popupSaveStatus==0){
		$("#backgroundSavePopup").css({
			"opacity": "0.7"
		});
		$("#backgroundSavePopup").fadeIn("slow");
		$("#popupSave").fadeIn("slow");
		popupSaveStatus = 1;
		var op = top.frames["main"].document.getElementById('pollOpen');
		$(op).val('false');
	}
}

//Desaparecer Popup Rechazo
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		var taTextArea = document.getElementById("rejectCommentsTA");
		$("#backgroundPopup").fadeOut("slow");
		$("#popupReject").fadeOut("slow");
		popupStatus = 0;
		//taTextArea.value = "";
	}
}

//Desaparecer Popup de Guardado
function disableSavePopup(){
	//disables popup only if it is enabled
	if(popupSaveStatus==1){
		$("#backgroundSavePopup").fadeOut("slow");
		$("#popupSave").fadeOut("slow");
		popupSaveStatus = 0;
	}
}

function disablePopupExit(){
	//disables popup only if it is enabled
	var bp = top.frames["sommaire"].document.getElementById('backgroundSavePopup');
	var pe = top.frames["sommaire"].document.getElementById('popupExit');

	$(bp).fadeOut("slow");
	$(pe).fadeOut("slow");
}

//Centrar Popup Rechazo
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupReject").height();
	var popupWidth = $("#popupReject").width();
	//centering
	$("#popupReject").css({
		"position": "absolute",
		"top": windowHeight-popupHeight-100,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
};

function getBrowserHeight()
{
	if (window.innerHeight)
	{
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight != 0)
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		return document.body.clientHeight;
	}	
	return 0;
};

//Centrar Popup de Guardado
function centerSavePopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupSave").height();
	var popupWidth = $("#popupSave").width();
	var scrollTop = $(window).scrollTop();
	//centering
	if(scrollTop > 0)
	{
		$("#popupSave").css({
			"position": "absolute",
			"top": 0,
			"left": windowWidth/2-popupWidth/2
		});
	}
	else
	{
		$("#popupSave").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
	}
	
	//only need force for IE6
	
	$("#backgroundSavePopup").css({
		"height": windowHeight
	});
	
}

function centerPopupExit(){
	//request data for centering
	var windowWidth = top.document.getElementById("frm2").contentWindow.document.documentElement.clientWidth;
	var windowHeight = getBrowserHeight();
	var pe = top.document.getElementById("frm2").contentWindow.document.getElementById('popupExit');
	var bp = top.document.getElementById("frm2").contentWindow.document.getElementById('backgroundSavePopup');
	var popupHeight = $(pe).height();
	var popupWidth = $(pe).width();
	var scrollTop = top.document.getElementById("frm2").contentWindow.document.body.scrollTop;
	var scrollLeft = top.document.getElementById("frm2").contentWindow.document.body.scrollLeft;
	
	
	if(scrollTop > 0)
	{
		$(pe).css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2 + scrollTop,
			"left": windowWidth/2-popupWidth/2
		});
	}
	else
	{
		$(pe).css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
	}
	
		
//	$(pe).css({
//		"position": "absolute",
//		"top": (windowHeight/2)-(popupHeight/2) + scrollTop,
//		"left": (windowWidth/2)-(popupWidth/2) + scrollLeft
//	});
	
	//only need force for IE6
	
	$(bp).css({
		"height": windowHeight
	});	
}

//CONTROLLING EVENTS IN jQuery

function printMSJE(msje, value)
{
	alert(msje + ": " + value);
}

$(function() {
	var offset = $("#popupSave").offset();
	var pe = top.document.getElementById("frm2").contentWindow.document.getElementById('popupExit');
	var peOffset = $(pe).offset();
	var windowHeight = getBrowserHeight();
	
	var scrollTopAnt = top.document.getElementById("frm2").contentWindow.document.body.scrollTop;	
    $(window).scroll(function() {
    	    	
    	var scrollTop = top.document.getElementById("frm2").contentWindow.document.body.scrollTop;    	
    	
//    	$("#popupExit").stop().animate({
//    		marginTop: scrollTop
//          });
    	
//    	if ($(pe).height() < windowHeight && peOffset != null && scrollTop > peOffset.top) {
    	   		
    	
//    	$(pe).stop().animate({
//    		marginTop: windowHeight/2 - $(pe).height()/2 + scrollTop
//		});
    	
//    		if ($(pe).height() < windowHeight && peOffset != null && scrollTop > peOffset.top)
//    		{
//    			
//    		}
//    		else
//    		{
    			$(pe).stop().animate({
    				marginTop: scrollTop
    			});
//    		}
          
		if ($("#popupSave").height() < $(window).height() && offset != null && $(window).scrollTop() > offset.top) {
    		$("#popupSave").stop().animate({
        		marginTop: $(window).height()/2 - $("#popupSave").height()/2 + $(window).scrollTop()
			});
		} else {
    		$("#popupSave").stop().animate({
       	 	marginTop: $(window).height()/2 - $("#popupSave").height()/2
		});
	};

    });
    	
});

$(document).ready(function(){
	
	$("#btnReject_button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupRejectClose").click(function(){
		disablePopup();
	});
	var pxc = top.frames["sommaire"].document.getElementById('popupExitClose');
	$(pxc).click(function(){
		disablePopupExit();
	});
	var benc = top.frames["sommaire"].document.getElementById('buttonExitNoChanges');
	$(benc).click(function(){
		var op = top.frames["main"].document.getElementById('pollOpen');
		var pr = top.frames["main"].document.getElementById('pageToRedirect');
		$(op).val('');
		disablePopupExit();
		window.open($(pr).val(),'sommaire');
	});	
	var besc = top.frames["sommaire"].document.getElementById('buttonExitSaveChanges');
	$(besc).click(function(){
		var op = top.frames["main"].document.getElementById('pollOpen');
		$(op).val('');
		disablePopupExit();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$("#buttonPopupSave").click(function(){
		disableSavePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});